package com.bsb.acc.controller;

import com.bsb.acc.domain.AccTopPageConfig;
import com.bsb.acc.dto.AccTopPageShowDto;
import com.bsb.acc.oms.web.domain.ResponseWrapper;
import com.bsb.acc.service.AccTopPageConfigService;
import com.bsb.acc.utils.FileUploadUtils;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * 置顶位配置controller
 *
 * @Author: ligc
 * @Date: 2019/2/13
 * @Description
 */
@Controller
@RequestMapping(value = "accTopPageConfig")
public class AccTopPageConfigController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private AccTopPageConfigService accTopPageConfigService;

    @RequestMapping(value = "uploadAccTopPage", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<AccTopPageConfig>> uploadAccTopPage(
            @RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        logger.info("[uploadImageUrl]上传图片：file={}", file);
        String imageType = request.getParameter("imageType");
        Integer type = Integer.parseInt(imageType);
        AccTopPageConfig accTopPageConfig = null;
        try {
            String returnDir = FileUploadUtils.uploadImage(file.getBytes());
            accTopPageConfig = accTopPageConfigService.save(returnDir, "user", type);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<AccTopPageConfig>>(
                ResponseWrapper.ok(accTopPageConfig).build(), HttpStatus.OK);
    }

    @RequestMapping(value = "publishAccTopPage", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<String>> publishAccTopPage()
            throws IOException {
        logger.info("置顶位配置");
        String user = null;
        try {
            accTopPageConfigService.publishAccTopPage(user);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<String>>(
                ResponseWrapper.ok("success").build(), HttpStatus.OK);
    }

    @RequestMapping(value = "cancelAccTopPage", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<String>> cancelAccTopPage()
            throws IOException {
        logger.info("取消发布");
        try {
            accTopPageConfigService.cancelAccTopPage();
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<String>>(
                ResponseWrapper.ok("success").build(), HttpStatus.OK);
    }

    @RequestMapping(value = "getAccTopPage", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<ResponseWrapper<AccTopPageShowDto>> getAccTopPage()
            throws IOException {
        logger.info("获取置顶背景信息用于展示");
        AccTopPageShowDto accTopPageShowDto = new AccTopPageShowDto();
        try {
            accTopPageShowDto = accTopPageConfigService.getAccTopPage();
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<ResponseWrapper<AccTopPageShowDto>>(
                ResponseWrapper.ok(accTopPageShowDto).build(), HttpStatus.OK);
    }


}
